package com.shiku.imserver.message.hander;

import com.shiku.imserver.common.annotation.MessageCommandAnnotation;
import com.shiku.imserver.common.hander.AbstractMessageHandler;
import com.shiku.imserver.common.message.PacketVO;
import com.shiku.imserver.common.message.PingMessage;
import com.shiku.imserver.common.packets.ImPacket;
import com.shiku.imserver.common.proto.MessageProBuf;
import com.shiku.imserver.common.utils.ProBufUtils;
import com.shiku.imserver.message.MessageFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;

public class PingMessageHandler extends AbstractMessageHandler {
    private static final Logger logger = LoggerFactory.getLogger(PingMessageHandler.class);

    @MessageCommandAnnotation(command = 99)
    public PacketVO pingMessageHandler(ImPacket packet, ChannelContext channelContext) {
        try {
            PingMessage message = (PingMessage) ProBufUtils.decoderMessageBody(packet.getBytes(), MessageProBuf.PingMessageProBuf.getDescriptor(), PingMessage.class);
            if (message != null) {
                //回复ping,客户端根据接收到的回复确认自己是否在线
                Tio.send(channelContext, MessageFactory.createSuccessPingPacket(message));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
